package com.youku.plugins.jump.view
{
   import flash.display.MovieClip;
   import com.youku.interfaces.IPlayerProxy;
   import com.youku.events.CoreEvent;
   import com.youku.events.PlayerControlEvent;
   import com.youku.events.ConductorEvent;
   import com.youku.interfaces.IPlayerData;
   import com.youku.events.PluginEvent;
   import com.youku.PlayerConfig;
   import com.youku.error.ErrorLevel;
   import com.youku.error.ErrorType;
   import com.youku.error.ErrorCode;
   
   public class JumpMediator extends Object
   {
      
      public function JumpMediator(param1:MovieClip, param2:IPlayerProxy) {
         super();
         this._mainMc = param1;
         this._playerProxy = param2;
         this.init();
      }
      
      public static const NAME:String = "JumpMediator";
      
      public static const JUMP_TAIL_NOTICE_BEFORE_TIME:Number = 20;
      
      private var _mainMc:MovieClip;
      
      private var _playerProxy:IPlayerProxy;
      
      private var _totalTime:Number;
      
      private var _lastNsTime:Number;
      
      private var _currentNsTime:Number;
      
      private var _headPosition:Number;
      
      private var _isHeadHintShowed:Boolean;
      
      private var _isJumpedHead:Boolean;
      
      private var _tailPosition:Number;
      
      private var _isTailHintShowed:Boolean;
      
      private var _isJumpedTail:Boolean;
      
      private function init() : void {
         this._lastNsTime = 0;
         this._currentNsTime = 0;
         this._isHeadHintShowed = false;
         this._isJumpedHead = false;
         this._isTailHintShowed = false;
         this._isJumpedTail = false;
         this._playerProxy.addEventListener(CoreEvent.PLAYING,this.onCorePlaying);
         this._playerProxy.addEventListener(PlayerControlEvent.COMMON_RESET,this.onReset);
         this._playerProxy.addEventListener(ConductorEvent.METADATA_UPDATED,this.onMetadataUpdated);
         this._playerProxy.addEventListener(PlayerControlEvent.COMMON_SET_JUMP,this.onPlayerControlEvent);
      }
      
      private function onReset(param1:PlayerControlEvent) : void {
         this._lastNsTime = 0;
         this._currentNsTime = 0;
         this._isHeadHintShowed = false;
         this._isTailHintShowed = false;
         this._isJumpedHead = false;
         this._isJumpedTail = false;
      }
      
      private function onPlayerControlEvent(param1:PlayerControlEvent) : void {
         var _loc2_:* = false;
         switch(param1.type)
         {
            case PlayerControlEvent.COMMON_SET_JUMP:
               _loc2_ = param1.data.jump as Boolean;
               this.checkForceJump(_loc2_);
               break;
         }
      }
      
      private function onMetadataUpdated(param1:ConductorEvent) : void {
         var _loc2_:IPlayerData = this._playerProxy.playerData;
         this._totalTime = _loc2_.playlistTotalTime;
      }
      
      private function onCorePlaying(param1:CoreEvent) : void {
         var _loc11_:String = null;
         var _loc12_:String = null;
         var _loc13_:String = null;
         var _loc14_:String = null;
         var _loc2_:Number = param1.data.nsTime;
         var _loc3_:Number = param1.data.loadedTime;
         this._currentNsTime = _loc2_;
         var _loc4_:Boolean = this._playerProxy.playerData.isJump;
         var _loc5_:Number = this._playerProxy.playerData.dvdinfo.tailPostion;
         var _loc6_:Number = _loc5_ / 1000;
         var _loc7_:* = false;
         var _loc8_:* = false;
         var _loc9_:* = false;
         var _loc10_:* = false;
         if(_loc2_ > _loc6_ - JUMP_TAIL_NOTICE_BEFORE_TIME && _loc2_ < _loc6_)
         {
            _loc7_ = true;
         }
         if(_loc2_ > _loc6_)
         {
            _loc8_ = true;
         }
         if(this._lastNsTime < _loc6_ && _loc2_ > _loc6_)
         {
            _loc9_ = true;
            if(_loc2_ < _loc6_ + 0.5)
            {
               _loc10_ = true;
            }
            else
            {
               _loc10_ = false;
               this._playerProxy.dispatchEvent(new PluginEvent(PluginEvent.JUMP_HIDE_INFO));
               this._isTailHintShowed = true;
               this._isJumpedTail = true;
            }
         }
         else
         {
            _loc9_ = true;
         }
         if(!(_loc5_ == -1) && !this._isTailHintShowed && (_loc7_))
         {
            this._isTailHintShowed = true;
            if(_loc4_)
            {
               this._playerProxy.dispatchEvent(new PluginEvent(PluginEvent.JUMP_SHOW_HINT_TAIL));
            }
            else
            {
               this._playerProxy.dispatchEvent(new PluginEvent(PluginEvent.JUMP_SHOW_HINT_TAIL_SETTING));
            }
         }
         if(!(_loc5_ == -1) && (_loc4_) && !this._isJumpedTail && (_loc8_) && (_loc9_) && (_loc10_))
         {
            this._isTailHintShowed = true;
            this._isJumpedTail = true;
            if(!PlayerConfig.isTudouPlayer)
            {
               this._playerProxy.dispatchEvent(new PluginEvent(PluginEvent.JUMP_TAIL));
            }
         }
         if((this._playerProxy.playerData.trialInfo) && (!(this._playerProxy.playerData.trialInfo.endTime == -1) && _loc2_ > this._playerProxy.playerData.trialInfo.endTime || this._playerProxy.playerData.trialInfo.endTime == -1 && _loc2_ >= this._playerProxy.playerData.videoTotalTime))
         {
            _loc11_ = ErrorLevel.FATAL_ERROR;
            _loc12_ = ErrorType.PAY_TRIAL_END_NOT_LOGIN;
            _loc13_ = ErrorCode.PAY_TRIAL_END_NOT_LOGIN;
            _loc14_ = "";
            this._playerProxy.dispatchEvent(new PluginEvent(PluginEvent.PLUGIN_ERROR,
               {
                  "level":_loc11_,
                  "message":_loc14_,
                  "type":_loc12_,
                  "code":_loc13_
               }));
         }
         this._lastNsTime = _loc2_;
      }
      
      private function checkForceJump(param1:Boolean) : void {
         if(param1 == false)
         {
            return;
         }
         if(PlayerConfig.isTudouPlayer)
         {
            return;
         }
         if(this._playerProxy.playerData == null || this._playerProxy.playerData.dvdinfo == null)
         {
            return;
         }
         var _loc2_:Number = this._playerProxy.playerData.dvdinfo.headPostion;
         var _loc3_:Number = _loc2_ / 1000;
         var _loc4_:Number = this._playerProxy.playerData.dvdinfo.tailPostion;
         var _loc5_:Number = _loc4_ / 1000;
         var _loc6_:* = false;
         var _loc7_:* = false;
         if(this._currentNsTime < _loc3_)
         {
            _loc6_ = true;
         }
         if(this._currentNsTime > _loc5_)
         {
            _loc7_ = true;
         }
         if(!(_loc2_ == -1) && (param1) && (_loc6_))
         {
            this._isHeadHintShowed = true;
            this._isJumpedHead = true;
            this._playerProxy.dispatchEvent(new PluginEvent(PluginEvent.JUMP_HEAD));
         }
         if(!(_loc4_ == -1) && (param1) && (_loc7_))
         {
            this._isTailHintShowed = true;
            this._isJumpedTail = true;
            this._playerProxy.dispatchEvent(new PluginEvent(PluginEvent.JUMP_TAIL));
         }
      }
   }
}
